<template>
  <div class="organizationManage">
    <header>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/specialUserManage' }" class="homePage">特殊用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>组织管理</el-breadcrumb-item>
      </el-breadcrumb>
      <p>组织管理</p>
    </header>
    <main>
      <div class="head">
        <button @click="addOrganization">添加</button>
      </div>
      <div class="body">
        <table cellspacing="0">
          <thead>
            <th>序号</th>
            <th>组织名称</th>
            <th>人员数</th>
            <th>操作</th>
          </thead>
          <tbody>
            <tr v-for="(item, indexNumber) in organizationList" :key="item.webDeptId">
              <td>{{ (departSearchDTO.index - 1) * departSearchDTO.pageSize + indexNumber + 1 }}</td>
              <td>{{ item.webDeptName }}</td>
              <td>{{ item.count || 0}}</td>
              <td>
                <span class="edit" @click="editOrganization(item.webDeptName, item.webDeptId)">编辑</span>
                <span class="delete" @click="deleteDepart(item)">删除</span>
              </td>
            </tr>
          </tbody>
        </table>
        <el-pagination
          v-if="total !== 0"
          @current-change="handleCurrentChange"
          :page-size="departSearchDTO.pageSize"
          layout="total, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
        <div class="empty"  v-show="organizationList.length === 0">
         <empty></empty>
        </div>
      </div>
    </main>
    <organizationWindow
      v-if="$store.state.ifShowCover"
      :title="title"
      :departName="departName"
      :departId="departId"
      @refresh="getConsumerList"
    ></organizationWindow>
    <delete-alert-box
    v-if="isDelete"
    :isDelete=isDelete
    :descTips=descTips
    @Confirm=Confirm
    @Cancel=Cancel
    ></delete-alert-box>
  </div>
</template>

<script>
import empty from '../../../components/empty'
import organizationWindow from '../components/organizationWindow'

import { deleteDepart } from '@/api/roleManage'
export default {
  data () {
    return {
      departSearchDTO: {
        index: 1,
        pageSize: 10
      },
      total: +'',
      organizationList: [],
      title: '',
      departName: '',
      departId: null,
      isDelete: false,
      descTips: [],
      ids: []
    }
  },
  components: {
    empty,
    organizationWindow
  },
  computed: {
    ifShowCover () {
      return this.$store.state.ifShowCover
    }
  },
  watch: {
    ifShowCover: {
      deep: true,
      handler (val) {
        if (val === false) {
          this.title = ''
          this.departName = ''
          this.departId = null
          this.getConsumerList()
        }
      }
    }
  },
  methods: {
    Confirm () {
      deleteDepart({ ids: this.ids }).then(res => {
        if (res.data.code === 200) {
          this.$message({
            type: 'success',
            message: res.data.message
          })
          this.isDelete = false
          this.getConsumerList()
        } else {
          this.$message({
            type: 'error',
            message: res.data.message
          })
        }
      })
    },
    Cancel () {
      this.isDelete = false
    },
    // 获取组织列表
    getConsumerList () {
      this.$store
        .dispatch('userManage/getConsumerList', this.departSearchDTO)
        .then(res => {
          this.organizationList = res.data.data.data
          this.total = res.data.data.count
        })
    },
    // 切换分页
    handleCurrentChange (val) {
      // console.log(val)
      this.departSearchDTO.index = val
      this.getConsumerList()
    },
    // 弹框的隐藏和显示
    showWindow () {
      this.$store.commit('set_ifShowCover', true)
    },
    // 点击添加按钮
    addOrganization () {
      this.title = '添加组织'
      this.showWindow()
    },
    // 点击编辑按钮
    editOrganization (name, id) {
      this.title = '编辑组织'
      this.departName = name
      this.departId = id
      this.showWindow()
    },
    // 删除组织
    deleteDepart (item) {
      this.ids = [item.webDeptId]
      this.descTips = [{
        tips: `确定删除组织"${item.webDeptName}"吗？`,
        subTips: '删除之后，该组织相关数据也会被删除。',
        default: ''
      }]
      this.isDelete = true
    }
  },
  created () {
    this.getConsumerList()
  }
}
</script>

<style lang="scss" scoped>
.organizationManage {
  overflow-y: auto;
  padding: 0 30px 30px;
  height: 100%;
  position: relative;
  header {
    height: 114px;

    .el-breadcrumb {
      height: 58px;
      line-height: 58px;
    }
    p {
      font-size: 24px;
      color: #333;
      font-weight: bold;
    }
  }
  main {
    background-color: #fff;
    border-radius: 5px;
    min-height: calc(100% - 114px);
    .head {
      height: 90px;
      border-bottom: 1px solid #f2f2f2;
      padding-left: 30px;
      line-height: 90px;
      button {
        width: 108px;
        height: 42px;
        background-color: #4868ff;
        color: #fff;
        font-size: 16px;
        border: none;
        border-radius: 21px;
        cursor: pointer;
      }
    }
    .body {
      padding: 0 30px 100px;
      position: relative;
      .empty{
        height: 617px;
      }
      table {
        margin-top: 20px;
        width: 100%;
        thead {
          th {
            background-color: rgba(87, 131, 255, 0.05);
            height: 60px;
            font-weight: normal;
            font-size: 14px;
            color: #333333;
            border-bottom: 1px solid #e6e6e6;
            text-align: left;
            padding-left: 24px;
          }
        }
        tbody {
          tr {
            td {
              color: #666666;
              font-size: 14px;
              text-align: left;
              padding-left: 24px;
              height: 60px;
              border-bottom: 1px solid #e6e6e6;
              span {
                text-decoration: underline;
                cursor: pointer;
              }
              .edit {
                color: #4868ff;
                margin-right: 24px;
              }
              .delete {
                color: #fb4646;
              }
            }
          }
        }
      }

      .el-pagination {
        position: absolute;
        right: 30px;
        bottom: 30px;
      }
    }
  }
  .organizationWindow {
    position: absolute;
    top: 114px;
    left: 320px;
    z-index: 999;
  }
}
</style>
